package com.example.littleken.sqlitetext;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioGroup;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity {
    private  EditText et_name,et_age,et_id;
    private RadioGroup genderGp;
    private String genderStr = "男";//用于保存用户输入的性别
    SQLiteDatabase db;
    private ListView stuList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_seoond_main);
        initView();
        //数据库创建
        //String path = Environment.getExternalStorageState() + "/test.db";
        //利用SQLiteOpenHelper类打开数据库
        //利用SQLiteDatabase类来控制数据库
        SQLiteOpenHelper helper = new SQLiteOpenHelper(SecondActivity.this,"test2.db",null,1) {
            @Override
            public void onCreate(SQLiteDatabase db) {
                Toast.makeText(SecondActivity.this,"数据库创建",Toast.LENGTH_SHORT).show();
                //在数据库创建同时，调用表创建方法db.execSQL()表创建
                String sql = "create table info_tb (_id integer primary key autoincrement," +
                        "name varhcar(20)," +
                        "age integer, "+
                        "gender varhcar(4) )";
                db.execSQL(sql);
            }

            @Override
            public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                Toast.makeText(SecondActivity.this,"数据库升级",Toast.LENGTH_SHORT).show();
            }
        };

        //利用SQLiteDatabase类来控制数据库
         db = helper.getReadableDatabase();
    }

    private void initView() {
        et_name = (EditText) findViewById(R.id.name_edt);
        et_age = (EditText) findViewById(R.id.age_edt);
        et_id = (EditText) findViewById(R.id.id_edt);
        genderGp = (RadioGroup) findViewById(R.id.gender_gp);
        stuList = (ListView) findViewById(R.id.stu_list);
        genderGp.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                if(checkedId == R.id.male){
                    genderStr = "男";
                }else{
                    genderStr = "女";
                }
            }
        });
    }

    public void operate(View v){
        String idStr = et_id.getText().toString();
        String nameStr = et_name.getText().toString();
        String ageStr = et_age.getText().toString();

        switch (v.getId()){
            case R.id.insert_btn:
                Toast.makeText(SecondActivity.this,"添加",Toast.LENGTH_SHORT).show();
                ContentValues values = new ContentValues();
                values.put("name",nameStr);
                values.put("age",ageStr);
                values.put("gender",genderStr);
                long id = db.insert("info_tb",null,values);
                Toast.makeText(SecondActivity.this,id+"",Toast.LENGTH_SHORT).show();
                break;
            case R.id.select_btn:
                Toast.makeText(SecondActivity.this,"查询",Toast.LENGTH_SHORT).show();

                Cursor c = db.query("info_tb",null,null,null,null,null,null);

                SimpleCursorAdapter adapter = new SimpleCursorAdapter(SecondActivity.this,R.layout.item,c,new String[]{
                        "_id","name","age","gender"},new int[]{
                        R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item
                }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
                //"id","name","age","gender"的数据填入到 R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item对应的控件中
                stuList.setAdapter(adapter);

                break;
            case R.id.delete_btn:
//                Toast.makeText(SecondActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                int count = db.delete("info_tb","_id=?",new String[]{
                        idStr
                });
                if(count > 0 ){
                    Toast.makeText(SecondActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(SecondActivity.this,"删除失败",Toast.LENGTH_SHORT).show();
                }

                Cursor cc = db.query("info_tb",null,null,null,null,null,null);

                SimpleCursorAdapter adaptersecond = new SimpleCursorAdapter(SecondActivity.this,R.layout.item,cc,new String[]{
                        "_id","name","age","gender"},new int[]{
                        R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item
                }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
                //"id","name","age","gender"的数据填入到 R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item对应的控件中
                stuList.setAdapter(adaptersecond);
                break;
            case R.id.update_btn:
//                Toast.makeText(SecondActivity.this,"修改",Toast.LENGTH_SHORT).show();
                ContentValues valuessecond = new ContentValues();
                valuessecond.put("name",nameStr);
                valuessecond.put("age",ageStr);
                valuessecond.put("gender",genderStr);
                int countsecond = db.update("info_tb",valuessecond,"_id=?",new String[]{
                        idStr
                });
                if(countsecond > 0){
                    Toast.makeText(SecondActivity.this,"修改成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(SecondActivity.this,"修改失败",Toast.LENGTH_SHORT).show();
                }

                Cursor ccc = db.query("info_tb",null,null,null,null,null,null);

                SimpleCursorAdapter adapterthird = new SimpleCursorAdapter(SecondActivity.this,R.layout.item,ccc,new String[]{
                        "_id","name","age","gender"},new int[]{
                        R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item
                }, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
                //"id","name","age","gender"的数据填入到 R.id.id_item,R.id.name_item,R.id.age_item,R.id.gender_item对应的控件中
                stuList.setAdapter(adapterthird);
            break;
        }
        et_id.setText("");
        et_name.setText("");
        et_age.setText("");
    }

}
